package org.jeecg.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pz.cloud.framework.base.dao.CommonDao;
import com.pz.cloud.framework.model.AbstractSqlBuilder;
import com.pz.cloud.framework.model.SqlBuilder;
import com.pz.cloud.framework.util.BizUtil;
import org.jeecg.common.handler.DataScopeHandler;
import org.jeecg.modules.system.entity.SysUserPosition;
import org.jeecg.modules.system.mapper.SysUserPostitionMapper;
import org.jeecg.modules.system.service.ISysUserPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class SysUserPositionServiceImpl extends ServiceImpl<SysUserPostitionMapper, SysUserPosition> implements ISysUserPositionService {

    @Autowired
    private CommonDao commonDao;

    @Override
    public List<SysUserPosition> findList(SysUserPosition sysUserPosition) {
        List list = commonDao.findList(sqlBuilder(sysUserPosition));
        return list;
    }

    @Override
    public void deleteByUserIdAndPostStatus(String userId, Integer status) {
        LambdaQueryWrapper<SysUserPosition> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysUserPosition::getUserId,userId);
        queryWrapper.eq(SysUserPosition::getPostStatus,status);
        remove(queryWrapper);
    }

    @Override
    public IPage<SysUserPosition> findPage(SysUserPosition sysUserPosition) {
        IPage page = commonDao.findPage(sqlBuilder(sysUserPosition));
        return page;
    }

    private AbstractSqlBuilder sqlBuilder(SysUserPosition entity) {
        DataScopeHandler dataScopeHandler = new DataScopeHandler()
                .setPersonalSql("p.user_id = :userId ").setPersonalParms("userId",BizUtil.getUserId());
        return SqlBuilder.init("select * from ").appendTable(entityClass).append(" p ")
                .joinCondition(entity, "p")
                .filterDataScope(dataScopeHandler)
                .returnType(entityClass);
    }
}
